﻿using System;
using System.Runtime.InteropServices;

namespace WinAPI
{
    abstract partial class LibWrapper
    {
        #region Constants
        private const uint FILE_SHARE_READ = 0x00000001;
        private const uint FILE_SHARE_WRITE = 0x00000002;
        private const uint FILE_SHARE_DELETE = 0x00000004;
        private const uint OPEN_EXISTING = 0x00000003;

        private const uint GENERIC_READ = 0x80000000;
        private const uint GENERIC_WRITE = 0x40000000;

        private const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
        private const uint FILE_READ_ATTRIBUTES = 0x0080;
        private const uint FILE_WRITE_ATTRIBUTES = 0x0100;
        private const uint ERROR_INSUFFICIENT_BUFFER = 0x122;

        private const uint DDD_RAW_TARGET_PATH = 0x1;
        private const uint DDD_REMOVE_DEFINITION = 0x2;

        private const uint IOCTL_FILE_DISK_CREATE_DEVICE = 2147508224;
        private const uint SCHNE_DRIVE_ADD = 0x100;
        private const uint SCHNF_PATH = 0x5;
        private const uint SCHNE_DRIVE_REMOVED = 0x80;

        private static IntPtr NULL = IntPtr.Zero;

        public const uint FILE_DEVICE_FILE_SYSTEM = 0x9;
        public const uint FSCTL_LOCK_VOLUME = 0x6;
        public const uint METHOD_BUFFERED = 0;
        public const uint FILE_ANY_ACCESS = 0;
        public const uint FSCTL_DISMOUNT_VOLUME = 0x8;
        public const uint MULTI_VDD_DEVICE = 0x8000;
        public const uint FILE_READ_ACCESS = 0x1;
        public const uint FILE_WRITE_ACCESS = 0x2;

        static public uint CTL_CODE(uint deviceType, uint funcion, uint method, uint access)
        {
            return ((deviceType << 16) | (access << 14) | (funcion << 2) | method);
        }

        #endregion

        #region externs
        
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateFile(
            string lpFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr lpSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool DeviceIoControl(
            IntPtr hDevice,
            uint dwIoControlCode,
            IntPtr lpInBuffer,
            int nInBufferSize,
            [Out] IntPtr lpOutBuffer,
            int nOutBufferSize,
            ref uint lpBytesReturned,
            IntPtr lpOverlapped);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool DefineDosDevice(
           uint dwFlags,
           string lpDeviceName,
           string lpTargetPath);

        [DllImport("shell32.dll")]
        private extern static void SHChangeNotify(
            uint eventID,
            uint flags,
            IntPtr item1,
            IntPtr item2);
        [DllImport("kernel32.dll")]
        public extern static bool DeleteVolumeMountPoint(
        [In]  string lpszVolumeMountPoint);
        #endregion
    
    }
}
